package com.tj.view.activity;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;

import com.tj.view.R;

/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class TsWindowMangerActivity extends BaseActivity{

    @Override
    public int getContentViewId() {
        return R.layout.activity_windowmanager;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
    }

    @Override
    public void initData() {
//        setContainer();
        createView();
    }

    private void createView(){
        ImageView myFV=new ImageView(getApplicationContext());
        myFV.setImageResource(R.drawable.ic_launcher);
        //获取WindowManager
        WindowManager wm=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        //设置LayoutParams(全局变量）相关参数
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        wmParams.type= WindowManager.LayoutParams.TYPE_PHONE;//设置window type
        wmParams.format= PixelFormat.RGBA_8888;   //设置图片格式，效果为背景透明
        //设置Window flag
        wmParams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity=Gravity.LEFT|Gravity.TOP;   //调整悬浮窗口至左上角
        //以屏幕左上角为原点，设置x、y初始值
        wmParams.x=0;
        wmParams.y=0;
        //设置悬浮窗口长宽数据
        wmParams.width=40;
        wmParams.height=40;
        //显示myFloatView图像
        wm.addView(myFV, wmParams);
    }

    // 设置容器，装载动画的view
    private void setContainer() {
        WindowManager mWm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        // 不允许触摸获取焦点
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;

        wmParams.gravity= Gravity.LEFT|Gravity.TOP;   //调整悬浮窗口至左上角

        // 设置图片格式，效果为背景透明
        wmParams.format = PixelFormat.RGBA_8888;
        //以屏幕左上角为原点，设置x、y初始值
        wmParams.x = 0;
        wmParams.y = 0;
        // 宽高适应内容
        wmParams.width = 90;
        wmParams.height = 90;
        // 如果view没有被加入到某个父组件中，则加入WindowManager中

        ImageView mImageView = new ImageView(this);
        mImageView.setImageDrawable(getDrawable(R.drawable.ic_launcher));
        // 如果view没有被加入到某个父组件中，则加入WindowManager中
        mWm.addView(mImageView, wmParams);
    }


}
